package nl.hu.verbrandendephoenix.controller;

import java.util.ArrayList;

import nl.hu.verbrandendephoenix.client.AppManager;
import nl.hu.verbrandendephoenix.client.NotificationManager;
import nl.hu.verbrandendephoenix.shared.Conversation;
import nl.hu.verbrandendephoenix.shared.Message;
import nl.hu.verbrandendephoenix.shared.User;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;

public class OpenConversation {
	private static final String DATE_FORMAT_DAY = "dd-MM-yyyy";
	private static final String DATE_FORMAT_HOUR = "HH";
	private static final String DATE_FORMAT_MINUTE = "mm";
	private NotificationManager nM = NotificationManager.get();
	private int index;
	private  nl.hu.verbrandendephoenix.view.OpenConversation view = new nl.hu.verbrandendephoenix.view.OpenConversation();
	private Conversation current_Conversation;
	private User current_User;
	private User conv_partner;
	private Message sending_message;
	private Message last_message;
	private ArrayList<Message> messages = new ArrayList<Message>();
	
	public OpenConversation() {
	}
	
	public Panel getPanel(int i) {
		System.out.println("validating user..");
		AppManager.get().getRpcService().getUserData(Cookies.getCookie("loginString"), new AsyncCallback<User>() {
			public void onFailure(Throwable caught) {
				System.out.println("Not good.");
				caught.printStackTrace();
				nM.commError();
			}
			public void onSuccess(User result) {
				if(result != null) {
					current_User = result;
				} else {
					nM.SID_Invalid();
				}
			}
		});
		
		Panel p = view.get();
		index = i;
		getConversationData();
		findConvPartner();
		addHandlers();
		return p;
	}
	
	private void addHandlers() {
		Button send = view.getSendButton();
		send.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				//Sending a message
				
				AppManager.get().getRpcService().storeMessage(current_Conversation.getCID(), view.getSendText().getText(), current_User, conv_partner, new AsyncCallback<Message>() {
					public void onFailure(Throwable caught) {
						nM.commError();
					}
					public void onSuccess(Message result) {
						if(result != null) {
							sending_message = result;
							sendMessage(sending_message);
							view.getSendText().setText("Type hier uw bericht..");
						} else {
							view.getConversationArea().setText("Text could not be sent.");
						}
					}
				});
				
				//sending_message = new Message(view.getSendText().getText(), day, hour, minute, current_User);	//date and time!
				//System.out.println(sending_message.toString());
			} 
		});
		
		TextBox sender = view.getSendText();
		sender.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				view.getSendText().setText("");
			} 
		});
	}
	
	private void fillData() {
		messages = current_Conversation.getMessages();
		int i = 0;
		for(Message m : messages) {
			sendMessage(m);
			view.getConvPartnerLabel().setText(view.getConvPartnerLabel().getText() + current_Conversation.getContact().getUserName());
			view.getConvPartnerLink().setHref("#User_" + current_Conversation.getContact().getDatabaseIndex());
			view.getConvPartnerLink().setText("Details");
		}
	}
	
	private void sendMessage(TextBox t) {
		if(last_message == null) {
			view.getConversationArea().setText(sending_message.getSender().getUserName() + ":\n" + sending_message.getText());
		} else if(last_message.getSender().getDatabaseIndex() == sending_message.getSender().getDatabaseIndex()){
			view.getConversationArea().setText(view.getConversationArea().getText() + "\n" + sending_message.getText());
		} else {
			view.getConversationArea().setText(view.getConversationArea().getText() + "\n\n" + sending_message.getSender().getUserName() + ":\n" + sending_message.getText());
		}
		last_message = sending_message;
	}
	
	private void sendMessage(Message m) {
		if(last_message == null) {
			view.getConversationArea().setText(m.getSender().getUserName() + ":\n" + m.getText());
		} else if(last_message.getSender().getDatabaseIndex() == m.getSender().getDatabaseIndex()){
			view.getConversationArea().setText(view.getConversationArea().getText() + "\n" + m.getText());
		} else {
			view.getConversationArea().setText(view.getConversationArea().getText() + "\n\n" + m.getSender().getUserName() + ":\n" + m.getText());
		}
		last_message = m;
	}
	
	private void findConvPartner() {
		
	}

	private void getConversationData() {
		System.out.println("Gettin conv data");
		AppManager.get().getRpcService().getConversationData(index, new AsyncCallback<Conversation>() {
			public void onFailure(Throwable caught) {
				nM.commError();
			}
			public void onSuccess(Conversation result) {
				if(result != null) {
					current_Conversation = result;
					fillData();
				} else {
					view.getConversationArea().setText("Conversation data could not be loaded - Be aware that chat data may not be received by conversation partner.");
				}
			}
		});
	}
}
